# -*- coding: utf-8 -*-
from flask import Blueprint, request, redirect, url_for
from flask import Blueprint, render_template
from common.models.bpm_data import BpmData
from common.libs.Helper import ops_renderJSON, iPagenation, ops_render
from controllers.index import index_page
import pandas as pd
from application import ExoskeletonEngine as engine
from echarts.stack_bar import static_bar
from echarts.base_bar import base_bar
from echarts.base_pie import base_pie
from echarts.reversal_bar import reversal_bar
from echarts.timeline_table import timeline_table
import datetime
from dateutil.relativedelta import relativedelta
from sqlalchemy.sql import func
from common.models.check_data import CheckData

check_page = Blueprint("check_page", __name__)

sql = ''' select * from check_data; '''
ordata = pd.read_sql_query(sql, engine)


@check_page.route("/index")
def checkindex():
    date = datetime.date.today() - relativedelta(months=+2)
    datestr = date.strftime("%Y%m")
    company = ["山西公司-大同公司", "山西公司-长治项目公司", "山西公司-太原公司", "山西公司-临汾项目公司", "地产集团西北区域山西公司", "地产集团西北区域公司"]
    years = datestr[:4]
    month = str(int(datestr[4:]))
    data = ordata[(ordata["company"].isin(company)) & (ordata["years"] == years) & (ordata["month"] == month)].loc[:, [
                                                                                                                          "company",
                                                                                                                          "absent",
                                                                                                                          "late",
                                                                                                                          "leave",
                                                                                                                          "sumvacation",
                                                                                                                          "overtime"

                                                                                                                      ]]

    value = data.groupby(['company']).agg(
        {"absent": 'sum', "late": 'sum', "leave": 'sum', "sumvacation": 'sum', "overtime": 'mean'}).reset_index().round(
        2).replace(["地产集团西北区域公司", "地产集团西北区域山西公司", "山西公司-临汾项目公司", "山西公司-大同公司", "山西公司-太原公司", "山西公司-长治项目公司"],
                   ["西北区域", "山西公司", "临汾项目公司", "大同公司", "太原公司", "长治项目公司"]).to_dict(orient='records')
    return ops_render("/check/index.html", {'value': value, "datestr": datestr, "part": "total"})


@check_page.route("/total")
def checktotal():
    date = request.args.get("date")
    years = date[:4]
    month = str(int(date[4:]))
    company = ["山西公司-大同公司", "山西公司-长治项目公司", "山西公司-太原公司", "山西公司-临汾项目公司", "地产集团西北区域山西公司", "地产集团西北区域公司"]
    data = ordata[(ordata["company"].isin(company)) & (ordata["years"] == years) & (ordata["month"] == month)].loc[:, [
                                                                                                                          "company",
                                                                                                                          "absent",
                                                                                                                          "late",
                                                                                                                          "leave",
                                                                                                                          "sumvacation",
                                                                                                                          "overtime"

                                                                                                                      ]]

    value = data.groupby(['company']).agg(
        {"absent": 'sum', "late": 'sum', "leave": 'sum', "sumvacation": 'sum', "overtime": 'mean'}).reset_index().round(
        2).replace(["地产集团西北区域公司", "地产集团西北区域山西公司", "山西公司-临汾项目公司", "山西公司-大同公司", "山西公司-太原公司", "山西公司-长治项目公司"],
                   ["西北区域", "山西公司", "临汾公司", "大同公司", "太原公司", "长治公司"]).to_dict(orient='records')
    return ops_render("/check/index.html", {'value': value, "datestr": date, "part": "total"})


@check_page.route("/leader")
def leader():
    date = request.args.get("date")
    return ops_render("check/leader.html", {"datestr": date, "part": "leader"})


@check_page.route("/api_check_total_fs")
def api_check_total_fs():
    date = request.args.get("date")
    years = date[:4]
    month = str(int(date[4:]))
    company = ["山西公司-大同公司", "山西公司-长治项目公司", "山西公司-太原公司", "山西公司-临汾项目公司", "地产集团西北区域山西公司", "地产集团西北区域公司"]

    data = ordata[(ordata["years"] == years) & (ordata["month"] == month) & (ordata["company"].isin(company))].loc[:, [
                                                                                                                          "company",
                                                                                                                          "absent",
                                                                                                                          "late",
                                                                                                                          "leave",
                                                                                                                          "sumvacation",
                                                                                                                          "num",
                                                                                                                          "days"

                                                                                                                      ]]
    data = data.groupby(['company']).agg(
        {"absent": 'sum', "late": 'sum', "leave": 'sum', "sumvacation": 'sum',
         "num": 'count', 'days': 'max'}).reset_index().round(2).replace(
        ["地产集团西北区域公司", "地产集团西北区域山西公司", "山西公司-临汾项目公司", "山西公司-大同公司", "山西公司-太原公司", "山西公司-长治项目公司"],
        ["西北区域", "山西公司", "临汾项目公司", "大同公司", "太原公司", "长治项目公司"])
    data["cq"] = (data["absent"] + data["sumvacation"]) / data["num"] * data["days"]
    data["cq"] = 100 - data["cq"]
    data["yc"] = (data["late"] + data["leave"]) / data["num"] * data["days"]

    x = data["company"].values.tolist()
    y = [data['yc'].round(2).tolist(), data['cq'].round(2).tolist()]
    legend = ["异常率", "出勤率"]
    color = ["#d87c7c", "#d7ab82"]
    stack = ["stack1", "stack2"]
    showdata = static_bar(x=x, y=y, title="出勤情况", legend=legend, color=color, rotate=0, stack=stack, isshow=True)
    return showdata.dump_options_with_quotes()


@check_page.route("/api_check_total_yc")
def api_check_total_yc():
    date = request.args.get("date")
    years = date[:4]
    month = str(int(date[4:]))
    company = ["山西公司-大同公司", "山西公司-长治项目公司", "山西公司-太原公司", "山西公司-临汾项目公司", "地产集团西北区域山西公司", "地产集团西北区域公司"]

    data = ordata[(ordata["years"] == years) & (ordata["month"] == month) & (ordata["company"].isin(company))].loc[:, [
                                                                                                                          "company",
                                                                                                                          "late",
                                                                                                                          "leave",

                                                                                                                      ]]
    data = data.groupby(['company']).agg(
        {"late": 'sum', "leave": 'sum'}).reset_index().round(
        2).replace(["地产集团西北区域公司", "地产集团西北区域山西公司", "山西公司-临汾项目公司", "山西公司-大同公司", "山西公司-太原公司", "山西公司-长治项目公司"],
                   ["西北区域", "山西公司", "临汾公司", "大同公司", "太原公司", "长治公司"])

    x = data["company"].values.tolist()
    y = [data['late'].tolist(), data['leave'].tolist()]
    legend = ["迟到天数", "早退天数"]
    color = ["#d87c7c","#efa18d"]
    stack = ["stack1", "stack1"]
    showdata = static_bar(x=x, y=y, title="考勤异常(单位：天)", legend=legend, color=color, rotate=0, stack=stack, isshow=False)
    return showdata.dump_options_with_quotes()


@check_page.route("/api_check_total_cq")
def api_check_total_cq():
    date = request.args.get("date")
    years = date[:4]
    month = str(int(date[4:]))
    company = ["山西公司-大同公司", "山西公司-长治项目公司", "山西公司-太原公司", "山西公司-临汾项目公司", "地产集团西北区域山西公司", "地产集团西北区域公司"]

    data = ordata[(ordata["years"] == years) & (ordata["month"] == month) & (ordata["company"].isin(company))].loc[:, [
                                                                                                                          "company",
                                                                                                                          "absent",
                                                                                                                          "sumvacation",

                                                                                                                      ]]
    data = data.groupby(['company']).agg(
        {"absent": 'sum', "sumvacation": 'sum'}).reset_index().round(
        2).replace(["地产集团西北区域公司", "地产集团西北区域山西公司", "山西公司-临汾项目公司", "山西公司-大同公司", "山西公司-太原公司", "山西公司-长治项目公司"],
                   ["西北区域", "山西公司", "临汾公司", "大同公司", "太原公司", "长治公司"])

    x = data["company"].values.tolist()
    y = [data['sumvacation'].tolist(), data['absent'].tolist()]
    legend = ["合计休假天数", "旷工天数"]
    color = ["#d7ab82", "#cc7e63"]
    stack = ["stack1", "stack1"]
    showdata = static_bar(x=x, y=y, title="出勤情况(单位：天)", legend=legend, color=color, rotate=0, stack=stack, isshow=False)
    return showdata.dump_options_with_quotes()


@check_page.route("/api_check_total_jb")
def api_check_total_jb():
    date = request.args.get("date")
    years = date[:4]
    month = str(int(date[4:]))
    company = ["山西公司-大同公司", "山西公司-长治项目公司", "山西公司-太原公司", "山西公司-临汾项目公司", "地产集团西北区域山西公司", "地产集团西北区域公司"]

    data = ordata[(ordata["years"] == years) & (ordata["month"] == month) & (ordata["company"].isin(company))].loc[:, [
                                                                                                                          "company",
                                                                                                                          "overtime"

                                                                                                                      ]]
    data = data.groupby(['company']).agg(
        {"overtime": 'mean'}).reset_index().round(
        2).replace(["地产集团西北区域公司", "地产集团西北区域山西公司", "山西公司-临汾项目公司", "山西公司-大同公司", "山西公司-太原公司", "山西公司-长治项目公司"],
                   ["西北区域", "山西公司", "临汾公司", "大同公司", "太原公司", "长治公司"])

    x = data["company"].values.tolist()
    y = data['overtime'].values.tolist()
    legend = "平均加班时长"
    color = "#919e8b"
    showdata = base_bar(x=x, y1=y, title="加班时长(单位：小时)", legend=legend, color=color, rotate=0)
    return showdata.dump_options_with_quotes()


@check_page.route("/api_check_leader_yc")
def api_check_leader_yc():
    date = request.args.get("date")
    years = date[:4]
    month = str(int(date[4:]))
    leader = ["傅立胜","赵娜","刘小利","苏景","王晋龙","于向春","赵乾","薛睿","原彬","耿毅","董俊卿","弋鸿雁","王昊元","肖一珍","燕瀚翔","张成生","王晶","陶俊辉","许晓芸","赵云川","向淑荣","刘志涛","邵衍存"]

    data = ordata[(ordata["years"] == years) & (ordata["month"] == month) & (ordata["name"].isin(leader))].loc[:, [
                                                                                                                      "name",

                                                                                                                      "late",
                                                                                                                      "leave",

                                                                                                                  ]]

    x = data["name"].values.tolist()
    y = [data['late'].tolist(), data['leave'].tolist()]
    legend = ["迟到天数", "早退天数"]
    color = ["#d87c7c", "#efa18d"]
    stack = ["stack1", "stack2"]
    showdata = static_bar(x=x, y=y, title="考勤异常(单位：天)", legend=legend, color=color, rotate=0, stack=stack, isshow=True)
    return showdata.dump_options_with_quotes()


@check_page.route("/api_check_leader_cq")
def api_check_leader_cq():
    date = request.args.get("date")
    years = date[:4]
    month = str(int(date[4:]))
    leader = ["傅立胜","赵娜","刘小利","苏景","王晋龙","于向春","赵乾","薛睿","原彬","耿毅","董俊卿","弋鸿雁","王昊元","肖一珍","燕瀚翔","张成生","王晶","陶俊辉","许晓芸","赵云川","向淑荣","刘志涛","邵衍存"]

    data = ordata[(ordata["years"] == years) & (ordata["month"] == month) & (ordata["name"].isin(leader))].loc[:, [
                                                                                                                      "name",
                                                                                                                      "absent",
                                                                                                                      "sumvacation",

                                                                                                                  ]]

    x = data["name"].values.tolist()
    y = [data['absent'].tolist(), data['sumvacation'].tolist()]
    legend = ["合计休假天数", "旷工天数"]
    color = ["#d7ab82", "#cc7e63"]
    stack = ["stack1", "stack2"]
    showdata = static_bar(x=x, y=y, title="出勤情况(单位：天)", legend=legend, color=color, rotate=0, stack=stack, isshow=True)
    return showdata.dump_options_with_quotes()


@check_page.route("/api_check_leader_jb")
def api_check_leader_jb():
    date = request.args.get("date")
    years = date[:4]
    month = str(int(date[4:]))
    leader = ["傅立胜","赵娜","刘小利","苏景","王晋龙","于向春","赵乾","薛睿","原彬","耿毅","董俊卿","弋鸿雁","王昊元","肖一珍","燕瀚翔","张成生","王晶","陶俊辉","许晓芸","赵云川","向淑荣","刘志涛","邵衍存"]

    data = ordata[(ordata["years"] == years) & (ordata["month"] == month) & (ordata["name"].isin(leader))].loc[:, [
                                                                                                                      "name",
                                                                                                                      "overtime",

                                                                                                                  ]]

    x = data["name"].values.tolist()
    y = data['overtime'].tolist()
    legend = "加班时长"
    color = "#919e8b"
    showdata = reversal_bar(x=x, y1=y, title="加班时长(单位：小时)", legend=legend, color=color)
    return showdata.dump_options_with_quotes()


@check_page.route("/overtime")
def overtimedata():
    date = request.args.get("date")
    return ops_render("/check/overtime.html", {"datestr": date, "part": "overtime"})


@check_page.route("/api_overtime_piedata1")
def overtimepiedata1():
    date = request.args.get("date")
    years = date[:4]
    month = str(int(date[4:]))
    company = ["地产集团西北区域公司"]

    data = ordata[(ordata["years"] == years) & (ordata["month"] == month) & (ordata["company"].isin(company))].loc[:, [
                                                                                                                          "company",
                                                                                                                          "dapartment",
                                                                                                                          "overtime"

                                                                                                                      ]]
    data = data.groupby(['dapartment']).agg(
        {"overtime": 'mean'}).reset_index().round(
        2).replace(["地产集团西北区域公司", "地产集团西北区域山西公司", "山西公司-临汾项目公司", "山西公司-大同公司", "山西公司-太原公司", "山西公司-长治项目公司"],
                   ["西北区域", "山西公司", "临汾公司", "大同公司", "太原公司", "长治公司"])
    data = data.drop(data[data['dapartment'] == "公司总助及以上"].index)

    x = data["dapartment"].values.tolist()
    y = data['overtime'].values.tolist()
    showdata = base_pie(x=x, y1=y, title="(单位：小时)")
    return showdata.dump_options_with_quotes()


@check_page.route("/api_overtime_piedata2")
def overtimepiedata2():
    date = request.args.get("date")
    years = date[:4]
    month = str(int(date[4:]))
    company = ["地产集团西北区域山西公司"]

    data = ordata[(ordata["years"] == years) & (ordata["month"] == month) & (ordata["company"].isin(company))].loc[:, [
                                                                                                                          "company",
                                                                                                                          "dapartment",
                                                                                                                          "overtime"

                                                                                                                      ]]
    data = data.groupby(['dapartment']).agg(
        {"overtime": 'mean'}).reset_index().round(
        2).replace(["地产集团西北区域公司", "地产集团西北区域山西公司", "山西公司-临汾项目公司", "山西公司-大同公司", "山西公司-太原公司", "山西公司-长治项目公司"],
                   ["西北区域", "山西公司", "临汾公司", "大同公司", "太原公司", "长治公司"])
    data = data.drop(data[data['dapartment'] == "公司总助及以上"].index)

    x = data["dapartment"].values.tolist()
    y = data['overtime'].values.tolist()
    showdata = base_pie(x=x, y1=y, title="(单位：小时)")
    return showdata.dump_options_with_quotes()

@check_page.route("/api_overtime_piedata3")
def overtimepiedata3():
    date = request.args.get("date")
    years = date[:4]
    month = str(int(date[4:]))
    company = ["山西公司-太原公司"]

    data = ordata[(ordata["years"] == years) & (ordata["month"] == month) & (ordata["company"].isin(company))].loc[:, [
                                                                                                                          "company",
                                                                                                                          "dapartment",
                                                                                                                          "overtime"

                                                                                                                      ]]
    data = data.groupby(['dapartment']).agg(
        {"overtime": 'mean'}).reset_index().round(
        2).replace(["地产集团西北区域公司", "地产集团西北区域山西公司", "山西公司-临汾项目公司", "山西公司-大同公司", "山西公司-太原公司", "山西公司-长治项目公司"],
                   ["西北区域", "山西公司", "临汾公司", "大同公司", "太原公司", "长治公司"])
    data = data.drop(data[data['dapartment'] == "公司总助及以上"].index)

    x = data["dapartment"].values.tolist()
    y = data['overtime'].values.tolist()
    showdata = base_pie(x=x, y1=y, title="(单位：小时)")
    return showdata.dump_options_with_quotes()


@check_page.route("/api_overtime_piedata4")
def overtimepiedata4():
    date = request.args.get("date")
    years = date[:4]
    month = str(int(date[4:]))
    company = ["山西公司-大同公司"]

    data = ordata[(ordata["years"] == years) & (ordata["month"] == month) & (ordata["company"].isin(company))].loc[:, [
                                                                                                                          "company",
                                                                                                                          "dapartment",
                                                                                                                          "overtime"

                                                                                                                      ]]
    data = data.groupby(['dapartment']).agg(
        {"overtime": 'mean'}).reset_index().round(
        2).replace(["地产集团西北区域公司", "地产集团西北区域山西公司", "山西公司-临汾项目公司", "山西公司-大同公司", "山西公司-太原公司", "山西公司-长治项目公司"],
                   ["西北区域", "山西公司", "临汾公司", "大同公司", "太原公司", "长治公司"])
    data = data.drop(data[data['dapartment'] == "公司总助及以上"].index)

    x = data["dapartment"].values.tolist()
    y = data['overtime'].values.tolist()
    showdata = base_pie(x=x, y1=y, title="(单位：小时)")
    return showdata.dump_options_with_quotes()

@check_page.route("/api_overtime_piedata5")
def overtimepiedata5():
    date = request.args.get("date")
    years = date[:4]
    month = str(int(date[4:]))
    company = ["山西公司-临汾项目公司"]

    data = ordata[(ordata["years"] == years) & (ordata["month"] == month) & (ordata["company"].isin(company))].loc[:, [
                                                                                                                          "company",
                                                                                                                          "dapartment",
                                                                                                                          "overtime"

                                                                                                                      ]]
    data = data.groupby(['dapartment']).agg(
        {"overtime": 'mean'}).reset_index().round(
        2).replace(["地产集团西北区域公司", "地产集团西北区域山西公司", "山西公司-临汾项目公司", "山西公司-大同公司", "山西公司-太原公司", "山西公司-长治项目公司"],
                   ["西北区域", "山西公司", "临汾公司", "大同公司", "太原公司", "长治公司"])
    data = data.drop(data[data['dapartment'] == "公司总助及以上"].index)

    x = data["dapartment"].values.tolist()
    y = data['overtime'].values.tolist()
    showdata = base_pie(x=x, y1=y, title="(单位：小时)")
    return showdata.dump_options_with_quotes()

@check_page.route("/api_overtime_piedata6")
def overtimepiedata6():
    date = request.args.get("date")
    years = date[:4]
    month = str(int(date[4:]))
    company = ["山西公司-长治项目公司"]

    data = ordata[(ordata["years"] == years) & (ordata["month"] == month) & (ordata["company"].isin(company))].loc[:, [
                                                                                                                          "company",
                                                                                                                          "dapartment",
                                                                                                                          "overtime"

                                                                                                                      ]]
    data = data.groupby(['dapartment']).agg(
        {"overtime": 'mean'}).reset_index().round(
        2).replace(["地产集团西北区域公司", "地产集团西北区域山西公司", "山西公司-临汾项目公司", "山西公司-大同公司", "山西公司-太原公司", "山西公司-长治项目公司"],
                   ["西北区域", "山西公司", "临汾公司", "大同公司", "太原公司", "长治公司"])
    data = data.drop(data[data['dapartment'] == "公司总助及以上"].index)

    x = data["dapartment"].values.tolist()
    y = data['overtime'].values.tolist()
    showdata = base_pie(x=x, y1=y, title="(单位：小时)")
    return showdata.dump_options_with_quotes()


@check_page.route("/cq")
def cq():
    date = request.args.get("date")
    return ops_render("/check/cq.html", {"datestr": date, "part": "cq"})


@check_page.route("/api_cq1")
def cq1():
    date = request.args.get("date")
    years = date[:4]
    month = str(int(date[4:]))
    company = ["地产集团西北区域公司"]

    data = ordata[(ordata["years"] == years) & (ordata["month"] == month) & (ordata["company"].isin(company))].loc[:, [
                                                                                                                          "company",
                                                                                                                          "dapartment",
                                                                                                                          "absent",
                                                                                                                          "sumvacation",

                                                                                                                      ]]
    data = data.groupby(['dapartment']).agg(
        {"absent": 'sum',"sumvacation": 'sum'}).reset_index().round(
        2).replace(["地产集团西北区域公司", "地产集团西北区域山西公司", "山西公司-临汾项目公司", "山西公司-大同公司", "山西公司-太原公司", "山西公司-长治项目公司"],
                   ["西北区域", "山西公司", "临汾公司", "大同公司", "太原公司", "长治公司"])
    data = data.drop(data[data['dapartment'] == "公司总助及以上"].index)

    x = data["dapartment"].values.tolist()
    y = [data['absent'].tolist(), data['sumvacation'].tolist()]
    legend = ["合计休假天数", "旷工天数"]
    color = ["#d7ab82", "#cc7e63"]
    stack = ["stack1", "stack2"]
    showdata = static_bar(x=x, y=y, title="(单位：天)", legend=legend, color=color, rotate=0, stack=stack, isshow=True)
    return showdata.dump_options_with_quotes()



@check_page.route("/api_cq2")
def cq2():
    date = request.args.get("date")
    years = date[:4]
    month = str(int(date[4:]))
    company = ["地产集团西北区域山西公司"]

    data = ordata[(ordata["years"] == years) & (ordata["month"] == month) & (ordata["company"].isin(company))].loc[:, [
                                                                                                                          "company",
                                                                                                                          "dapartment",
                                                                                                                          "absent",
                                                                                                                          "sumvacation",

                                                                                                                      ]]
    data = data.groupby(['dapartment']).agg(
        {"absent": 'sum',"sumvacation": 'sum'}).reset_index().round(
        2).replace(["地产集团西北区域公司", "地产集团西北区域山西公司", "山西公司-临汾项目公司", "山西公司-大同公司", "山西公司-太原公司", "山西公司-长治项目公司"],
                   ["西北区域", "山西公司", "临汾公司", "大同公司", "太原公司", "长治公司"])
    data = data.drop(data[data['dapartment'] == "公司总助及以上"].index)

    x = data["dapartment"].values.tolist()
    y = [data['absent'].tolist(), data['sumvacation'].tolist()]
    legend = ["合计休假天数", "旷工天数"]
    color = ["#d7ab82", "#cc7e63"]
    stack = ["stack1", "stack2"]
    showdata = static_bar(x=x, y=y, title="(单位：天)", legend=legend, color=color, rotate=0, stack=stack, isshow=True)
    return showdata.dump_options_with_quotes()


@check_page.route("/api_cq3")
def cq3():
    date = request.args.get("date")
    years = date[:4]
    month = str(int(date[4:]))
    company = ["山西公司-太原公司"]

    data = ordata[(ordata["years"] == years) & (ordata["month"] == month) & (ordata["company"].isin(company))].loc[:, [
                                                                                                                          "company",
                                                                                                                          "dapartment",
                                                                                                                          "absent",
                                                                                                                          "sumvacation",

                                                                                                                      ]]
    data = data.groupby(['dapartment']).agg(
        {"absent": 'sum',"sumvacation": 'sum'}).reset_index().round(
        2).replace(["地产集团西北区域公司", "地产集团西北区域山西公司", "山西公司-临汾项目公司", "山西公司-大同公司", "山西公司-太原公司", "山西公司-长治项目公司"],
                   ["西北区域", "山西公司", "临汾公司", "大同公司", "太原公司", "长治公司"])
    data = data.drop(data[data['dapartment'] == "公司总助及以上"].index)

    x = data["dapartment"].values.tolist()
    y = [data['absent'].tolist(), data['sumvacation'].tolist()]
    legend = ["合计休假天数", "旷工天数"]
    color = ["#d7ab82", "#cc7e63"]
    stack = ["stack1", "stack2"]
    showdata = static_bar(x=x, y=y, title="(单位：天)", legend=legend, color=color, rotate=0, stack=stack, isshow=True)
    return showdata.dump_options_with_quotes()

@check_page.route("/api_cq4")
def cq4():
    date = request.args.get("date")
    years = date[:4]
    month = str(int(date[4:]))
    company = ["山西公司-大同公司"]

    data = ordata[(ordata["years"] == years) & (ordata["month"] == month) & (ordata["company"].isin(company))].loc[:, [
                                                                                                                          "company",
                                                                                                                          "dapartment",
                                                                                                                          "absent",
                                                                                                                          "sumvacation",

                                                                                                                      ]]
    data = data.groupby(['dapartment']).agg(
        {"absent": 'sum',"sumvacation": 'sum'}).reset_index().round(
        2).replace(["地产集团西北区域公司", "地产集团西北区域山西公司", "山西公司-临汾项目公司", "山西公司-大同公司", "山西公司-太原公司", "山西公司-长治项目公司"],
                   ["西北区域", "山西公司", "临汾公司", "大同公司", "太原公司", "长治公司"])
    data = data.drop(data[data['dapartment'] == "公司总助及以上"].index)

    x = data["dapartment"].values.tolist()
    y = [data['absent'].tolist(), data['sumvacation'].tolist()]
    legend = ["合计休假天数", "旷工天数"]
    color = ["#d7ab82", "#cc7e63"]
    stack = ["stack1", "stack2"]
    showdata = static_bar(x=x, y=y, title="(单位：天)", legend=legend, color=color, rotate=0, stack=stack, isshow=True)
    return showdata.dump_options_with_quotes()

@check_page.route("/api_cq5")
def cq5():
    date = request.args.get("date")
    years = date[:4]
    month = str(int(date[4:]))
    company = ["山西公司-临汾项目公司"]

    data = ordata[(ordata["years"] == years) & (ordata["month"] == month) & (ordata["company"].isin(company))].loc[:, [
                                                                                                                          "company",
                                                                                                                          "dapartment",
                                                                                                                          "absent",
                                                                                                                          "sumvacation",

                                                                                                                      ]]
    data = data.groupby(['dapartment']).agg(
        {"absent": 'sum',"sumvacation": 'sum'}).reset_index().round(
        2).replace(["地产集团西北区域公司", "地产集团西北区域山西公司", "山西公司-临汾项目公司", "山西公司-大同公司", "山西公司-太原公司", "山西公司-长治项目公司"],
                   ["西北区域", "山西公司", "临汾公司", "大同公司", "太原公司", "长治公司"])
    data = data.drop(data[data['dapartment'] == "公司总助及以上"].index)

    x = data["dapartment"].values.tolist()
    y = [data['absent'].tolist(), data['sumvacation'].tolist()]
    legend = ["合计休假天数", "旷工天数"]
    color = ["#d7ab82", "#cc7e63"]
    stack = ["stack1", "stack2"]
    showdata = static_bar(x=x, y=y, title="(单位：天)", legend=legend, color=color, rotate=0, stack=stack, isshow=True)
    return showdata.dump_options_with_quotes()

@check_page.route("/api_cq6")
def cq6():
    date = request.args.get("date")
    years = date[:4]
    month = str(int(date[4:]))
    company = ["山西公司-长治项目公司"]

    data = ordata[(ordata["years"] == years) & (ordata["month"] == month) & (ordata["company"].isin(company))].loc[:, [
                                                                                                                          "company",
                                                                                                                          "dapartment",
                                                                                                                          "absent",
                                                                                                                          "sumvacation",

                                                                                                                      ]]
    data = data.groupby(['dapartment']).agg(
        {"absent": 'sum',"sumvacation": 'sum'}).reset_index().round(
        2).replace(["地产集团西北区域公司", "地产集团西北区域山西公司", "山西公司-临汾项目公司", "山西公司-大同公司", "山西公司-太原公司", "山西公司-长治项目公司"],
                   ["西北区域", "山西公司", "临汾公司", "大同公司", "太原公司", "长治公司"])
    data = data.drop(data[data['dapartment'] == "公司总助及以上"].index)

    x = data["dapartment"].values.tolist()
    y = [data['absent'].tolist(), data['sumvacation'].tolist()]
    legend = ["合计休假天数", "旷工天数"]
    color = ["#d7ab82", "#cc7e63"]
    stack = ["stack1", "stack2"]
    showdata = static_bar(x=x, y=y, title="(单位：天)", legend=legend, color=color, rotate=0, stack=stack, isshow=True)
    return showdata.dump_options_with_quotes()



@check_page.route("/yc")
def yc():
    date = request.args.get("date")
    return ops_render("/check/yc.html", {"datestr": date, "part": "yc"})


@check_page.route("/api_yc1")
def yc1():
    date = request.args.get("date")
    years = date[:4]
    month = str(int(date[4:]))
    company = ["地产集团西北区域公司"]

    data = ordata[(ordata["years"] == years) & (ordata["month"] == month) & (ordata["company"].isin(company))].loc[:, [
                                                                                                                          "company",
                                                                                                                          "dapartment",
                                                                                                                          "late",
                                                                                                                          "leave",

                                                                                                                      ]]
    data = data.groupby(['dapartment']).agg(
        {"late": 'sum',"leave": 'sum'}).reset_index().round(
        2).replace(["地产集团西北区域公司", "地产集团西北区域山西公司", "山西公司-临汾项目公司", "山西公司-大同公司", "山西公司-太原公司", "山西公司-长治项目公司"],
                   ["西北区域", "山西公司", "临汾公司", "大同公司", "太原公司", "长治公司"])
    data = data.drop(data[data['dapartment'] == "公司总助及以上"].index)

    x = data["dapartment"].values.tolist()
    y = [data['late'].tolist(), data['leave'].tolist()]
    legend = ["迟到次数", "早退次数"]
    color = ["#d87c7c", "#efa18d"]
    stack = ["stack1", "stack2"]
    showdata = static_bar(x=x, y=y, title="(单位：次)", legend=legend, color=color, rotate=0, stack=stack, isshow=True)
    return showdata.dump_options_with_quotes()



@check_page.route("/api_yc2")
def yc2():
    date = request.args.get("date")
    years = date[:4]
    month = str(int(date[4:]))
    company = ["地产集团西北区域山西公司"]

    data = ordata[(ordata["years"] == years) & (ordata["month"] == month) & (ordata["company"].isin(company))].loc[:, [
                                                                                                                          "company",
                                                                                                                          "dapartment",
                                                                                                                          "late",
                                                                                                                          "leave",

                                                                                                                      ]]
    data = data.groupby(['dapartment']).agg(
        {"late": 'sum', "leave": 'sum'}).reset_index().round(
        2).replace(["地产集团西北区域公司", "地产集团西北区域山西公司", "山西公司-临汾项目公司", "山西公司-大同公司", "山西公司-太原公司", "山西公司-长治项目公司"],
                   ["西北区域", "山西公司", "临汾公司", "大同公司", "太原公司", "长治公司"])
    data = data.drop(data[data['dapartment'] == "公司总助及以上"].index)

    x = data["dapartment"].values.tolist()
    y = [data['late'].tolist(), data['leave'].tolist()]
    legend = ["迟到次数", "早退次数"]
    color = ["#d87c7c", "#efa18d"]
    stack = ["stack1", "stack2"]
    showdata = static_bar(x=x, y=y, title="(单位：次)", legend=legend, color=color, rotate=0, stack=stack, isshow=True)
    return showdata.dump_options_with_quotes()


@check_page.route("/api_yc3")
def yc3():
    date = request.args.get("date")
    years = date[:4]
    month = str(int(date[4:]))
    company = ["山西公司-太原公司"]

    data = ordata[(ordata["years"] == years) & (ordata["month"] == month) & (ordata["company"].isin(company))].loc[:, [
                                                                                                                          "company",
                                                                                                                          "dapartment",
                                                                                                                          "late",
                                                                                                                          "leave",

                                                                                                                      ]]
    data = data.groupby(['dapartment']).agg(
        {"late": 'sum', "leave": 'sum'}).reset_index().round(
        2).replace(["地产集团西北区域公司", "地产集团西北区域山西公司", "山西公司-临汾项目公司", "山西公司-大同公司", "山西公司-太原公司", "山西公司-长治项目公司"],
                   ["西北区域", "山西公司", "临汾公司", "大同公司", "太原公司", "长治公司"])
    data = data.drop(data[data['dapartment'] == "公司总助及以上"].index)

    x = data["dapartment"].values.tolist()
    y = [data['late'].tolist(), data['leave'].tolist()]
    legend = ["迟到次数", "早退次数"]
    color = ["#d87c7c", "#efa18d"]
    stack = ["stack1", "stack2"]
    showdata = static_bar(x=x, y=y, title="(单位：次)", legend=legend, color=color, rotate=0, stack=stack, isshow=True)
    return showdata.dump_options_with_quotes()

@check_page.route("/api_yc4")
def yc4():
    date = request.args.get("date")
    years = date[:4]
    month = str(int(date[4:]))
    company = ["山西公司-大同公司"]

    data = ordata[(ordata["years"] == years) & (ordata["month"] == month) & (ordata["company"].isin(company))].loc[:, [
                                                                                                                          "company",
                                                                                                                          "dapartment",
                                                                                                                          "late",
                                                                                                                          "leave",
                                                                                                                      ]]
    data = data.groupby(['dapartment']).agg(
        {"late": 'sum', "leave": 'sum'}).reset_index().round(
        2).replace(["地产集团西北区域公司", "地产集团西北区域山西公司", "山西公司-临汾项目公司", "山西公司-大同公司", "山西公司-太原公司", "山西公司-长治项目公司"],
                   ["西北区域", "山西公司", "临汾公司", "大同公司", "太原公司", "长治公司"])
    data = data.drop(data[data['dapartment'] == "公司总助及以上"].index)

    x = data["dapartment"].values.tolist()
    y = [data['late'].tolist(), data['leave'].tolist()]
    legend = ["迟到次数", "早退次数"]
    color = ["#d87c7c", "#efa18d"]
    stack = ["stack1", "stack2"]
    showdata = static_bar(x=x, y=y, title="(单位：次)", legend=legend, color=color, rotate=0, stack=stack, isshow=True)
    return showdata.dump_options_with_quotes()

@check_page.route("/api_yc5")
def yc5():
    date = request.args.get("date")
    years = date[:4]
    month = str(int(date[4:]))
    company = ["山西公司-临汾项目公司"]

    data = ordata[(ordata["years"] == years) & (ordata["month"] == month) & (ordata["company"].isin(company))].loc[:, [
                                                                                                                          "company",
                                                                                                                          "dapartment",
                                                                                                                          "late",
                                                                                                                          "leave",

                                                                                                                      ]]
    data = data.groupby(['dapartment']).agg(
        {"late": 'sum', "leave": 'sum'}).reset_index().round(
        2).replace(["地产集团西北区域公司", "地产集团西北区域山西公司", "山西公司-临汾项目公司", "山西公司-大同公司", "山西公司-太原公司", "山西公司-长治项目公司"],
                   ["西北区域", "山西公司", "临汾公司", "大同公司", "太原公司", "长治公司"])
    data = data.drop(data[data['dapartment'] == "公司总助及以上"].index)

    x = data["dapartment"].values.tolist()
    y = [data['late'].tolist(), data['leave'].tolist()]
    legend = ["迟到次数", "早退次数"]
    color = ["#d87c7c", "#efa18d"]
    stack = ["stack1", "stack2"]
    showdata = static_bar(x=x, y=y, title="(单位：次)", legend=legend, color=color, rotate=0, stack=stack, isshow=True)
    return showdata.dump_options_with_quotes()

@check_page.route("/api_yc6")
def yc6():
    date = request.args.get("date")
    years = date[:4]
    month = str(int(date[4:]))
    company = ["山西公司-长治项目公司"]

    data = ordata[(ordata["years"] == years) & (ordata["month"] == month) & (ordata["company"].isin(company))].loc[:, [
                                                                                                                          "company",
                                                                                                                          "dapartment",
                                                                                                                          "late",
                                                                                                                          "leave",

                                                                                                                      ]]
    data = data.groupby(['dapartment']).agg(
        {"late": 'sum', "leave": 'sum'}).reset_index().round(
        2).replace(["地产集团西北区域公司", "地产集团西北区域山西公司", "山西公司-临汾项目公司", "山西公司-大同公司", "山西公司-太原公司", "山西公司-长治项目公司"],
                   ["西北区域", "山西公司", "临汾公司", "大同公司", "太原公司", "长治公司"])
    data = data.drop(data[data['dapartment'] == "公司总助及以上"].index)

    x = data["dapartment"].values.tolist()
    y = [data['late'].tolist(), data['leave'].tolist()]
    legend = ["迟到次数", "早退次数"]
    color = ["#d87c7c", "#efa18d"]
    stack = ["stack1", "stack2"]
    showdata = static_bar(x=x, y=y, title="(单位：次)", legend=legend, color=color, rotate=0, stack=stack, isshow=True)
    return showdata.dump_options_with_quotes()